<?php

namespace App\Http\Controllers\Admin\View;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Facades\Services\CommodityService;
use ValiDate;

class CommodityController extends Controller
{
    public function index(Request $request){

        $conditions = [];
        if(isset($request->ct_type_id) && $request->ct_type_id != ""){
            $rules = [
                'ct_type_id' => 'exists:commodity_types,id'
            ];

            ValiDate::valiDate($request,$rules);

        
            $conditions['ct_type_id'] = $request->ct_type_id;
        }

    	return view('pages.admin.commodity.list',[
            'commodities' => CommodityService::getCommodities($conditions),
            'commodityTypes' => CommodityService::getTypes(),
            'conditions' => $conditions
        ]);
    }

    public function create(Request $request){
    	return view('pages.admin.commodity.create',[
            'commodityTypes' => CommodityService::getTypes()
        ]);
    }

    public function update(Request $request){
        $rules = [
            'id' => 'required|exists:commodities,id'
        ];

        ValiDate::valiDate($request,$rules);

        $data = $request->all();

    	return view('pages.admin.commodity.update',[
            'commodity' => CommodityService::getCommodity($request->id),
            'commodityTypes' => CommodityService::getTypes()
        ]);
    }

    public function details(Request $request){
        $rules = [
            'id' => 'required|exists:commodities,id'
        ];

        ValiDate::valiDate($request,$rules);

        $data = $request->all();

    	return view('pages.admin.commodity.details',[
            'commodity' => CommodityService::getCommodity($request->id)
        ]);
    }
}
